//
//  renewalCardRemindViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/2.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  续卡提醒

#import "RenewalCardRemindViewController.h"
#import "YZPullDownMenu.h"
#import "VipCardClassSelectViewController.h"
#import "VipCardStateSelectViewController.h"
#import "YZMenuButton.h"
#import "VipInfoCardTableViewCell.h"
#import "ChangeRemindViewController.h"

@interface RenewalCardRemindViewController ()<YZPullDownMenuDataSource,UITableViewDelegate,UITableViewDataSource>{
    
    int startIndex;
    
    int pageSize;
}

@property (nonatomic, strong) NSArray *titles;

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,weak)UILabel *detailLabel;

@property(nonatomic,strong)NSString *searchCond;

@property(nonatomic,strong)NoDataView *noDataView;

@property(nonatomic,strong)VipCardClassSelectViewController *vc1;

@end

@implementation RenewalCardRemindViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.view.backgroundColor = BackGroundColor;
    
    self.title = @"续卡提醒";
    
    [self createSubView];
    
    [NotificationCenter addObserver:self selector:@selector(loadStartData) name:@"ReloadRenwalCard" object:nil];
    
    [NotificationCenter addObserver:self selector:@selector(conditionClick:) name:YZUpdateMenuTitleNote object:nil];
}

-(void)createSubView{
    
    UIView *topWhiteView = [[UIView alloc] init];
    topWhiteView.backgroundColor = CSWhiteColor;
    [self.view addSubview:topWhiteView];
    [topWhiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.width.mas_equalTo(self.view);
    }];
    
    UILabel *noteLabel = [[UILabel alloc] init];
    noteLabel.textColor = CSColorFromRGB(0x7a7a7a);
    noteLabel.text = @"显示符合以下条件的会员卡：";
    noteLabel.font = SmallFont;
    [topWhiteView addSubview:noteLabel];
    [noteLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topWhiteView).offset(20);
        make.left.mas_equalTo(topWhiteView).offset(10);
    }];
    
    UILabel *detailLabel = [[UILabel alloc] init];
    detailLabel.text = @"储值卡<500元，次卡<5次，有效期<15天";
    detailLabel.font = MidFont;
    detailLabel.numberOfLines = 0;
    detailLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [topWhiteView addSubview:detailLabel];
    self.detailLabel = detailLabel;
    [detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(noteLabel);
        make.top.mas_equalTo(noteLabel.mas_bottom).offset(15);
        make.bottom.mas_equalTo(topWhiteView).offset(-15);
    }];
    
    UIButton *changeBtn = [[UIButton alloc] init];
    [changeBtn setImage:@"card_icon_modify"];
    changeBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [changeBtn setTitle:@"修改条件"];
    [changeBtn setTitleColor:CSColorFromRGB(0x333333)];
    [topWhiteView addSubview:changeBtn];
    [changeBtn addTarget:self action:@selector(changeCondition)];
    [changeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(topWhiteView).offset(-15);
        make.centerY.mas_equalTo(topWhiteView);
    }];
    [changeBtn titleBelowTheImageWithSpace:10];
    
    UIImageView *lineView = [[UIImageView alloc] init];
    lineView.image = [UIImage imageNamed:@"card_icon_li"];
    [topWhiteView addSubview:lineView];
    [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topWhiteView).offset(5);
        make.bottom.mas_equalTo(topWhiteView).offset(-5);
        make.width.mas_equalTo(1);
        make.right.mas_equalTo(changeBtn.mas_left).offset(-10);
    }];
    
    // 创建下拉菜单
    YZPullDownMenu *menu = [[YZPullDownMenu alloc] init];
    //menu.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    menu.coverColor = [CSBlackColor colorWithAlphaComponent:0.4];
    menu.backgroundColor = CSWhiteColor;
    [self.view addSubview:menu];
    [menu mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topWhiteView.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
        make.left.width.mas_equalTo(self.view);
    }];
    
    // 设置下拉菜单代理
    menu.dataSource = self;
    
    // 初始化标题
    _titles = @[@"会员卡种类",@"会员卡状态"];
    
    // 添加子控制器
    [self setupAllChildViewController];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[VipInfoCardTableViewCell class] forCellReuseIdentifier:@"VipInfoCardTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = CSClearColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.automaticallyHidden = YES;
    
    
    MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
    tableView.mj_header = mj_head;
    tableView.mj_footer = footer;
    [tableView.mj_header beginRefreshing];
    
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.width.mas_equalTo(self.view);
        make.top.mas_equalTo(menu.mas_bottom);
    }];
}

#pragma mark 下拉
-(void)loadStartData{
    
    startIndex = 1;
    
    pageSize = 20;
    
    [self loadCardData];
}

#pragma mark 上拉
-(void)loadMoreData{
    
    startIndex = startIndex + 1;
    
    [self loadCardData];
}

-(void)loadCardData{
    
    //GET_Leave_DeleteLeave
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    VenueModel *venueModel = [VenueModel venue];
    
//    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];
    
    [params setObject:@(true) forKey:@"IsNotify"];
    
    [params setValue:self.searchCond forKey:@"Condition"];
    
    [params setObject:[NSString stringWithFormat:@"%d",startIndex] forKey:@"page"];
    
    [params setObject:[NSString stringWithFormat:@"%d",pageSize] forKey:@"rows"];
    
    [HTTPTool GET:GET_SalesOrder_SalesOrderListData parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count > 0) {
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshing];
                  }
                  else{
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                  }
                  
                  self.noDataView = [NoDataView shareInstance];
                  
                  [self.noDataView hideInView:self.view];
                  
                  if (startIndex == 1) {
                      
                      self.dataSource = [NSMutableArray arrayWithArray:resultArray];
                      
                      if (self.dataSource.count == 0) {
                          
                          self.noDataView = [NoDataView shareInstance];
                          
                          [self.noDataView showInView:self.view];
                      }
                  }
                  else{
                      
                      [self.dataSource addObjectsFromArray:resultArray];
                  }
                  
                  self.vc1.dataSource = self.dataSource;
                  
                  [self.tableView reloadData];
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}

-(void)loadData{
    
    //Get_GetMemberCardNotifySetListData
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    VenueModel *venueModel = [VenueModel venue];
    
//    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];
    
    [params setObject:[NSString stringWithFormat:@"%d",1] forKey:@"page"];
    
    [params setObject:[NSString stringWithFormat:@"%d",100] forKey:@"rows"];
    
    [HTTPTool GET:GET_SalesOrder_SalesOrderListData parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count == 0) {
                      
                      self.detailLabel.text = @"没有条件";
                  }
                  else{
                      
                      //储值卡<500元，次卡<5次，有效期<15天
                      NSDictionary *dict = [resultArray firstObject];
                      
                      NSString *times = [dict objectForKey:@"Times"];
                      
                      NSString *balance = [dict objectForKey:@"Balance"];
                      
                      NSString *validityDay = [dict objectForKey:@"ValidityDay"];
                      
                      self.detailLabel.text = [NSString stringWithFormat:@"储值卡<%@元，次卡<%@次，有效期<%@天",[balance isEmptyString]?@"0":balance,[times isEmptyString]?@"0":balance,[validityDay isEmptyString]?@"0":validityDay];
                  }
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}


#pragma mark - 添加子控制器
- (void)setupAllChildViewController
{
    VipCardClassSelectViewController *vc1 = [[VipCardClassSelectViewController alloc] init];
    vc1.colCount = 0;
    self.vc1 = vc1;
    
    VipCardStateSelectViewController *vc2 = [[VipCardStateSelectViewController alloc] init];
    vc2.colCount = 1;
    vc2.titleArray = [NSMutableArray arrayWithObjects:@"默认",@"正常",@"请假中",@"已停卡",@"已过期", nil];
    
    [self addChildViewController:vc1];
    [self addChildViewController:vc2];
}

#pragma mark - YZPullDownMenuDataSource
// 返回下拉菜单多少列
- (NSInteger)numberOfColsInMenu:(YZPullDownMenu *)pullDownMenu
{
    return _titles.count;
}

// 返回下拉菜单每列按钮
- (UIButton *)pullDownMenu:(YZPullDownMenu *)pullDownMenu buttonForColAtIndex:(NSInteger)index
{
    YZMenuButton *button = [YZMenuButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = MidFont;
    [button setTitle:_titles[index] forState:UIControlStateNormal];
    [button setTitleColor:CSBlackColor forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:25 /255.0 green:143/255.0 blue:238/255.0 alpha:1] forState:UIControlStateSelected];
    [button setImage:[UIImage imageNamed:@"more_icon_arrow_down"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"more_icon_arrow_up"] forState:UIControlStateSelected];
    
    return button;
}

// 返回下拉菜单每列对应的控制器
- (UIViewController *)pullDownMenu:(YZPullDownMenu *)pullDownMenu viewControllerForColAtIndex:(NSInteger)index
{
    return self.childViewControllers[index];
}

// 返回下拉菜单每列对应的高度
- (CGFloat)pullDownMenu:(YZPullDownMenu *)pullDownMenu heightForColAtIndex:(NSInteger)index
{
    // 第1列 高度
    if (index == 0) {
        
        return self.view.height;
    }
    
    return 220;
    
}

#pragma mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = CSWhiteColor;
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = MidBigFont;
    titleLabel.text = @"会员卡种数：";
    titleLabel.textColor = TextColor1;
    [headView addSubview:titleLabel];
    [titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(headView).offset(15);
        make.centerY.mas_equalTo(headView);
    }];
    
    UILabel *countLabel = [[UILabel alloc] init];
    countLabel.font = MidBigFont;
    countLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.dataSource.count];
    [headView addSubview:countLabel];
    [countLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(titleLabel);
        make.left.mas_equalTo(titleLabel.mas_right).offset(5);
    }];
    return headView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VipInfoCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VipInfoCardTableViewCell"];
    
    NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
    
    VipCardModel *vipCardModel = [VipCardModel mj_objectWithKeyValues:dict];
    
    cell.vipCardModel = vipCardModel;
    
    cell.selectRow = indexPath.row;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}


#pragma mark 搜索条件
-(void)conditionClick:(NSNotification *)sender{
    
    NSDictionary *userInfo = sender.userInfo;
    
    NSString *title = [userInfo objectForKey:@"title"];
    
    self.searchCond = title;
    
    [self loadStartData];
}

#pragma mark 修改条件
-(void)changeCondition{
    
    ChangeRemindViewController *changeRemidVC = [[ChangeRemindViewController alloc] init];
    [self.navigationController pushViewController:changeRemidVC animated:YES];
}

-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
        
    }
    
    return _dataSource;
}


-(void)dealloc{
    
    [NotificationCenter removeObserver:self];
}

@end
